//
//  LoginViewController.m
//  XXX
//
//  Created by Xiaoshiheng_pro on 2017/2/17.
//  Copyright © 2017年 Xiaoshiheng_pro. All rights reserved.
//
#include <ifaddrs.h>
#include <arpa/inet.h>


#import "Server.h"
#import "Client.h"
#import "LoginViewController.h"
// 引入Swift头文件
#import "XXX-Swift.h"


#define kRGB(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1]

@interface LoginViewController ()<UIScrollViewDelegate,ClientDelegate,ServerDelegate>

@property (nonatomic,assign) BOOL isServer;



@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isServer = NO;
    self.ServerUrl.text = [self deviceIPAdress];
    
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
//    self.scrollView.bounces = NO;
//    self.scrollView.scrollEnabled = NO;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [self performSelector:@selector(scrollViewDidEndScrollingAnimation:) withObject:nil afterDelay:0.3];

}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    
    NSInteger index = fabs(self.scrollView.contentOffset.x)/ self.scrollView.frame.size.width;
    
    self.qiehuanSegmented.selectedSegmentIndex = index;
    
    if (index == 1) {
        self.LoginCkickBut.backgroundColor = kRGB(253, 146, 38);
    }else{
        self.LoginCkickBut.backgroundColor = kRGB(11, 88, 135);
    }
    
}

- (IBAction)segmentedControlAction:(id)sender {
    
    UISegmentedControl  *seg = sender;
    NSInteger index =  seg.selectedSegmentIndex;
    
    if (index == 1) {
        [self.scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:YES];
        self.isServer = YES;
        
        self.LoginCkickBut.backgroundColor = kRGB(253, 146, 38);
    }else{
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        self.isServer = NO;
        
        self.LoginCkickBut.backgroundColor = kRGB(11, 88, 135);
    }
    
}

- (IBAction)LoginCkick:(id)sender {
    
    if (self.isServer) {
       
//        Server *serverSocket =
        [Server ServerInit];
        
        
        CT *test = [[CT alloc] init];
        [test logServerWithGOViewController:self];
        
    }else{
        Client *clientSocket =  [Client ClientInit];
        clientSocket.delegate = self;
        [clientSocket startConnectToHost:self.TextUrl.text];
    }
}

- (NSString *)deviceIPAdress {
    NSString *address = @"an error occurred when obtaining ip address";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    
    success = getifaddrs(&interfaces);
    
    if (success == 0) { // 0 表示获取成功
        
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if( temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            
            temp_addr = temp_addr->ifa_next;
        }
    }
    
    freeifaddrs(interfaces);
    
    NSLog(@"设备的IP是：%@", address);
    return address;
}

///建立连接
- (void)clientSocket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
    
    // 使用Swift的类
    CT *test = [[CT alloc] init];
    [test logClientWithGOViewController:self];
    
}

@end
